﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FlowBasis.Security.Passwords;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace FlowBasisUnitTests.Security.Passwords
{
    [TestClass]
    public class Sha512PasswordValidationServiceTests
    {
        [TestMethod]
        public void Should_Generate_Random_Salt()
        {
            var passwordService = new Sha512PasswordValidationService();

            byte[] salt1 = passwordService.GenerateSalt(64);
            byte[] salt2 = passwordService.GenerateSalt(64);

            Assert.AreEqual(64, salt1.Length);
            Assert.AreEqual(64, salt2.Length);

            bool differenceFound = false;
            for (int co = 0; co < 64; co++)
            {
                if (salt1[co] != salt2[co])
                    differenceFound = true;                
            }
            Assert.IsTrue(differenceFound);
        }

        [TestMethod]
        public void Should_Generate_Hash_And_Validate_Password()
        {
            var passwordService = new Sha512PasswordValidationService();

            byte[] saltCorrect = passwordService.GenerateSalt(64);
            byte[] saltWrong = passwordService.GenerateSalt(64);

            byte[] hash = passwordService.ComputeHash("hello", saltCorrect);

            bool result = passwordService.ValidatePassword("hello", saltCorrect, hash);
            Assert.IsTrue(result);

            result = passwordService.ValidatePassword("hello", saltWrong, hash);
            Assert.IsFalse(result);

            result = passwordService.ValidatePassword("helloWrong", saltCorrect, hash);
            Assert.IsFalse(result);
        }
    }
}
